# Copyright 2024 PKU-Alignment Team. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

import hashlib
import json
import os
import shutil
from datetime import datetime


def get_timestamp() -> str:
    return datetime.now().strftime('%Y%m%d-%H%M%S')


def generate_hash_uid(to_hash: dict | tuple | list | str) -> str:
    """Generates a unique hash for a given model and arguments."""
    # Convert the dictionary to a JSON string
    json_string = json.dumps(to_hash, sort_keys=True)

    # Generate a hash of the JSON string
    hash_object = hashlib.sha256(json_string.encode())
    return hash_object.hexdigest()


def copy_file(source_path, destination_path):
    try:
        if not os.path.exists(source_path):
            raise FileNotFoundError(f'Source file does not exist: {source_path}')
        if os.path.exists(destination_path):
            raise FileExistsError(f'Destination file already exists: {destination_path}')

        os.makedirs(os.path.dirname(destination_path), exist_ok=True)
        shutil.copy2(source_path, destination_path)

    except Exception as e:
        print(f'Error copying file: {str(e)}')
